package com.kpiCount;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class KpiCountService {
    private static final Logger logger = Logger.getLogger(KpiCountService.class.getName());
    @PostConstruct
    public void init() {
       new Thread(new Runnable() {
           @Override
           public void run() {
               KpiCountService.run();
           }
       }).start();
    }
    public static void run(){
        logger.info("---------kpi count start-----------");
        EventLoopGroup bossGroup = new NioEventLoopGroup();

        EventLoopGroup workGroup = new NioEventLoopGroup();

        try {

            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(bossGroup, workGroup);

            bootstrap.channel(NioServerSocketChannel.class);

            bootstrap.childHandler(new ChildChannelHandler());

            Channel channel = bootstrap.bind(8939).sync().channel();

            channel.closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }


    }
}
